//window 
#pragma once
#include <iostream>
#include <GL/glfw.h>
#include <sstream> 

class Window {
private:
	std::string m_title;
	int m_Width, m_Height, m_redbits, m_greenbits, m_bluebits, m_alphabits, m_depthbits, m_stencialbits, m_mode;
	bool m_running;
	double m_timer_current_frame, m_timer_previous_frame;

public:
	Window() {
		m_title = "default windows constructor";
		m_Width = 300;
		m_Height = 300;
		m_redbits = 0;
		m_greenbits = 0;
		m_bluebits = 0;
		m_alphabits = 0;
		m_depthbits = 0;
		m_stencialbits = 0;
		m_mode = GLFW_WINDOW;
		m_running = true;
		
	} 

	Window(std::string s, int w, int h, int rb, int gb, int bb, int ab, int db, int sb, int md) {
		m_title = s;
		m_Width = w;
		m_Height = h;
		m_redbits = rb;
		m_greenbits = gb;
		m_bluebits = bb;
		m_alphabits = ab;
		m_depthbits = db;
		m_stencialbits = sb;
		m_mode = md;
		m_running = true;
		
	} 

	~Window() { 
		glfwTerminate();
	}


	std::string getTitle() { return m_title; }
	void setTitle(std::string x) { m_title = x; }

	bool OpenWindow()
	{
		if(!glfwOpenWindow(m_Width, m_Height, m_redbits, m_greenbits, m_bluebits, m_alphabits, m_depthbits, m_stencialbits, m_mode))
		{
			return false; 
		}

		return true;
	}

	bool init()
	{
		if(!glfwInit())
		{
			return false;
		}

		if(!OpenWindow())
		{
			return false;
		}

		m_timer_previous_frame = glfwGetTime();
		m_timer_current_frame = m_timer_previous_frame; 

		return true;
	}

	double deltaTime() { return m_timer_current_frame- m_timer_previous_frame; };


	void updateWindow() { 
		if(glfwGetWindowParam(GLFW_OPENED) > 0)
		{
			m_running = true;
		}else {
			m_running = false;	
		}
	}

	void clearWindow()
	{
		glClear(GL_COLOR_BUFFER_BIT);

	}

	void swapBuffers()
	{
		glfwSwapBuffers();
		m_timer_previous_frame = m_timer_current_frame;
		m_timer_current_frame = glfwGetTime();


	}
	
	void setRuning(bool s)
	{
		if(s == true)
		{
			m_running = false;
		}
	}

	bool isRunning()
	{
		return m_running;
	}

	void updateTitle()
	{
		glfwSetWindowTitle(getTitle().c_str());
	}

	template<typename T>void updateTitle(T s)
	{
		std::stringstream st;

		st << s;
		this->setTitle(st.str());
		glfwSetWindowTitle(getTitle().c_str());
	}

};
